package com.vertx.verticles;

import com.vertx.codec.OtherCodec;
import com.vertx.model.dto.OtherDTO;
import com.vertx.model.vo.OtherVO;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;

/**
 * @name: OtherVerticle
 * @description: OtherVerticle
 * @author: jiangcs
 */
public class OtherVerticle extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        OtherCodec otherCodec = new OtherCodec();

        EventBus eventBus = vertx.eventBus();

        // 订阅
        eventBus.consumer("vertx.other", (msg) -> {
            if (msg.body() instanceof OtherVO) {
                OtherVO otherVO = (OtherVO) msg.body();
                System.out.println("msg => otherVO: " + otherVO.getUserName());
            }
            System.out.println("msg: " + msg.body());
        });

        // 配置编解码
        eventBus.registerCodec(otherCodec);
        DeliveryOptions deliveryOptions = new DeliveryOptions();
        deliveryOptions.setCodecName(otherCodec.name());
        // 发送
        OtherDTO otherDTO = new OtherDTO();
        otherDTO.setUserName("vert.x");
        eventBus.send("vertx.other", otherDTO, deliveryOptions);
    }
}
